package com.example.myapplication01.chapter05;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.myapplication01.R;

import java.util.Random;

public class LoginForgetActivity extends AppCompatActivity implements View.OnClickListener {
    Button btn_verifycode;
    Button btn_confirm;
    EditText et_password_first;
    EditText et_password_second;
    EditText et_verifycode;

    String mVerifyCode;//验证码
    String phone;//手机号

    String mPhone;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_forget);
        // 从上一个页面获取要修改密码的手机号码
        mPhone = getIntent().getStringExtra("phone");
        btn_verifycode=findViewById(R.id.btn_verifycode);
        btn_confirm=findViewById(R.id.btn_confirm);
        et_verifycode=findViewById(R.id.et_verifycode);
        et_password_first=findViewById(R.id.et_password_first);
        et_password_second=findViewById(R.id.et_password_second);
        btn_verifycode.setOnClickListener( this);
        btn_confirm.setOnClickListener( this);

    }

    @Override
    public void onClick(View v) {
//        点击了获取验证码
        if (v.getId()==R.id.btn_verifycode){
            // 生成六位随机数字的验证码
            mVerifyCode = String.format("%06d", new Random().nextInt(999999));
            // 以下弹出提醒对话框，提示用户记住六位验证码数字
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            // 设置对话框的标题文本
            builder.setTitle("验证码");
            // 设置对话框的内容文本
            builder.setMessage("手机号" + phone + ",本次验证码是" + mVerifyCode + ",请输入验证码");
            // 设置对话框的肯定按钮文本及其点击监听器
            builder.setPositiveButton("ok", null);
            // 根据建造器构建提醒对话框对象
            AlertDialog dialog = builder.create();
            // 显示提醒对话框
            dialog.show();
        }
//        点击了确定按钮
        if(v.getId()==R.id.btn_confirm){
            String password_first = et_password_first.getText().toString();
            String password_second = et_password_second.getText().toString();
            if (password_first.length() < 6) {
                Toast.makeText(this, "请输入正确的密码", Toast.LENGTH_SHORT).show();
                return;
            }

            if (!password_first.equals(password_second)) {
                Toast.makeText(this, "两次输入的新密码不一致", Toast.LENGTH_SHORT).show();
                return;
            }

            if (!mVerifyCode.equals(et_verifycode.getText().toString())) {
                Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
                return;
            }
            Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show();
            // 以下把修改好的新密码返回给上一个页面
            Intent intent = new Intent();
            intent.putExtra("new_password", password_first);
            setResult(Activity.RESULT_OK, intent);
            finish();
        }
    }
}